package demo2;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

class Student implements Comparable<Student>{
    public String name ;
    public int age ;
    public Student(String name ,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "Student{"+"name=" + name +'\'' + ", age=" + age +'}';
    }

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
//        return this.age - o.age;
        /*if(this.age > o.age) {
            return 1;
        }else if(this.age == o.age) {
            return 0;
        }else {
            return -1;
        }*/
    }

}

class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;

    }

}
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}



public class Test {

    public static void main22(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",31);

        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));

        System.out.println("=====");

        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",50);
        students[1] = new Student("lisi",31);
        students[2] = new Student("abc",59);

        NameComparator nameComparator2 = new NameComparator();
        AgeComparator ageComparator2 = new AgeComparator();

        Arrays.sort(students,ageComparator2);
        System.out.println(Arrays.toString(students));
    }

    public static void mySort(Comparable[] comparables) {

        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                //if(comparables[j] > comparables[j+1]) {
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    //交换
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }

    public static void main21(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",50);
        students[1] = new Student("lisi",31);
        students[2] = new Student("abc",59);
//        Arrays.sort(students);
        mySort(students);
        System.out.println(Arrays.toString(students));
    }
    public static void main20(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("lisi",18);
        if(student1.compareTo(student2) > 0) {
            System.out.println("student1 > student2");
        }else {
            System.out.println("student1 <= student2");
        }
    }
    public static void main13(String[] args) {
        String str = "name=zhagnsan&age=10";
        //实现多次分割
        String[] ret = str.split("&");
        //[0]:name=zhagnsan   [1]:age=10
        for(String x : ret) {
            //System.out.println(x);
            String[] s = x.split("=");
            for(String ss : s) {
                System.out.println(ss);
            }
        }
    }
    public static void main12(String[] args) {
        String str = "192\\168\\1\\1";
        //System.out.println(str);
        //正则表达式的分割
        String[] ret = str.split("\\\\");

        for(int i = 0; i < ret.length;i++) {
            System.out.println(ret[i]);
        }
        System.out.println("========================");
        String str2 = "name=zhagnsan&age=10";
        String[] ret2 = str2.split("=|&");
        for(int i = 0; i < ret2.length;i++) {
            System.out.println(ret2[i]);
        }
    }

    public static void main11(String[] args) {
        //String str = "name=zhagnsan&age=10";
        //String str = "hello world hello bit";
        String str = "192.168.1.1";
        //正则表达式的分割
        String[] ret = str.split("\\.");

        for(int i = 0; i < ret.length;i++) {
            System.out.println(ret[i]);
        }

    }

    public static void main10(String[] args) {
        String str = "ababcabcdabcdef";
        //替换所有的老的字符为第2个参数
        String ret = str.replace('a','p');
        System.out.println(ret);
        //可以替换 字符串
        String ret2 = str.replace("ab","ppppp");
        System.out.println(ret2);

        String ret3 = str.replaceAll("ab","uuuu");
        System.out.println(ret3);

        String ret4 = str.replaceFirst("ab","kkk");
        System.out.println(ret4);

        System.out.println(str);
    }

    public static void main9(String[] args) {
        String str = "hello";
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            System.out.println(ch);
        }


        String s = String.format("%d-%d-%d", 2019, 9,14);
        System.out.println(s);
    }

    public static void main8(String[] args) {

        String str = "hello";
        String ret = str.toUpperCase();
        System.out.println(ret);

        System.out.println(str);


        String str2 = "HELLO";
        String ret2 = str2.toLowerCase();
        System.out.println(ret2);

    }

    public static void main7(String[] args) {
        //把整数 转化成 字符串
        String str = String.valueOf(123);
        System.out.println(str);
        //这个过程  你可以理解为 序列化 |||   序列化与反序列化
//        String str2 = String.valueOf(new Student("张三",11));
//        System.out.println(str2);

        //包装类： 首先是一个类 其次 类中 拥有更多的方法  使用起来 非常的方便
        int a = Integer.parseInt("123");
        //int a1 = Integer.valueOf("123");
        System.out.println(a+12);





    }

    public static void main6(String[] args) {
        String s1 = new String("hello");

        int index3 = s1.lastIndexOf('l');
        System.out.println(index3);
        int index4 = s1.lastIndexOf('l',1);
        System.out.println(index4);
        System.out.println("======");

        //返回字符串对应下标的字符
        char ch = s1.charAt(1);
        System.out.println(ch);

        //返回对应字符出现的下标位置  从头开始一个一个查
        //int index = s1.indexOf('l');
        //从指定位置开始找某个字符出现的位置
        int index = s1.indexOf('l',3);
        System.out.println(index);


        //字符串查找 从一个字符串找另一个字符串
        int index2 = s1.indexOf("llo");
        System.out.println(index2);


    }

    public static void main5(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");

        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));

        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareToIgnoreCase(s2));


    }
    //判断字符串的大小
    public static void main4(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hz");

        //s1和s2比较 大小  s1 > s2 返回大于0的数字   s1 < s2 返回 小于0 的数字  否则返回0
        System.out.println(s1.compareTo(s2));



    }
    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hz");

        System.out.println(s1 == s2);

        System.out.println(s1.equals(s2));//被String重写了


        String s3 = "hello";
        String s4 = "hz";
        System.out.println(s3 == s4);
        System.out.println(s3.equals(s4));

    }

    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;
        System.out.println(s3);

        System.out.println(s1.length());

        String str4 = "";
        System.out.println("输出："+str4);
        System.out.println(str4.length());
        System.out.println(str4.isEmpty());

        String str5 = null;
        System.out.println(str5.isEmpty());

    }



    public static void main1(String[] args) {
        String str = "hello";

        String str2 = new String("world");
        System.out.println(str);
        System.out.println(str2);

        char[] array = {'a','b','c'};
        String str3 = new String(array);
        System.out.println(str3);

    }

    public static void main111(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("world");
        String s4 = new String("world");
        System.out.println(s1==s2);
        System.out.println(s1.equals(s2));
        System.out.println(s3 == s4);

    }

    public static void main112(String[] args) {

        Person person1  = new Person("zhangsan",18);
        Person person2  = new Person("zhangsan",18);

        int a = 10;
        int b = 10;

        System.out.println(a == b);//true
        System.out.println(person1 == person2);//false  直接用 == 比较，比较的是地址
        System.out.println(person1.equals(person2));//true  重写equals方法了

        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }


}
class OutClass {
    private int a;
    static int b;
    public void methodA() {
        a = 10;
        System.out.println(a);
    }
    public static void methodB() {
        System.out.println(b);
    }

    //静态内部类
    static class InnerClass {
        //在内部类中只能访问外部类的静态成员
        public void methodinner() {
           // a = 100;//error a不是类的成员变量
            b = 200;
           // methodA();//error 它不是类的成员方法
            methodB();
        }
    }
    public static void main113(String[] args) {
        OutClass.InnerClass innerClass = new OutClass.InnerClass();//获取静态内部类对象 点好优先级最高，先跟后面结合
        innerClass.methodinner();//成员访问
        OutClass outClass = new OutClass();
        System.out.println(outClass.a);
//        System.out.println(a);
    }


}
class OutClass2{
    private int a = 2;
    static int b = 3;
    int c = 4;
    public void methodA() {
        a = 10;
        System.out.println(a);
    }
    public static void methodB() {
        System.out.println(b);
    }

    //实例内部类
    class InnerClass {
        int c = 5;
        public static final int d = 6;//在实例内部类中 被static修饰的成员变量要加 final并且赋值，这时候把它看作是一个常量。常量是在编译的时候确定的
        public void methodinner() {
            //在实例内部类中可以直接访问外部类中任意访问限定符修饰的成员
            a = 100;
            b = 200;
            methodA();
            methodB();

            //如果外部类和实例内部类有相同名字的成员，优先访问内部类自己的
            System.out.println(c);

            //如果要访问外部类同名成员，必须：外部类名.this.同名的成员名
            OutClass2.this.c = 400;
            System.out.println(OutClass2.this.c);

        }

    }

    public static void main114(String[] args) {
        OutClass2 outClass2 = new OutClass2();
        System.out.println(outClass2.a);
        System.out.println(OutClass2.b);
        System.out.println(outClass2.c);
        outClass2.methodA();
        outClass2.methodB();

        //创建实例内部类对象
        OutClass2.InnerClass innerClass = new OutClass2().new InnerClass();
        innerClass.methodinner();
    }
}
class Person{
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;//不是person类对象
        Person person = (Person) o;//向下转型，比较属性值
//      return age == person.age && Objects.equals(name, person.name);
        return this.name.equals(person.name) && this.age == person.age;//这样写也可以
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    private String name;
    private int age;
    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
}
